package com.example.sqlitedrugs;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainDrugsActivity extends Activity {
	EditText inputSearch;
	private static final String TAG = "MainDrugsActivity";

	private MySQLiteOpenHelper dataHelper;

	private Cursor cursor; // tuong duong voi mang
	private SimpleCursorAdapter adapter; // tuong duong voi adapter
	private ListView listDrugs;
	ArrayList<HashMap<String, String>> array;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_drugs);
		

		dataHelper = new MySQLiteOpenHelper(MainDrugsActivity.this);
		listDrugs = (ListView) findViewById(R.id.lv);

		LoadData();
		String sql = "select * from " + MySQLiteOpenHelper.TB_DRUGS + " where "
				+ MySQLiteOpenHelper.CL_id + " >1 and "
				+ MySQLiteOpenHelper.CL_id + " <47";

		Log.d(TAG, sql);

		cursor = dataHelper.SELECTSQL(sql);

		while (cursor.moveToNext()) {

			String id = cursor.getString(cursor
					.getColumnIndex(MySQLiteOpenHelper.CL_id));

			if (Integer.parseInt(id) % 2 == 0) {
				// neu cot dang xet co id la chan
				// lay name va verb
				String name = cursor.getString(cursor
						.getColumnIndex(MySQLiteOpenHelper.CL_Name));
				String Address = cursor.getString(cursor
						.getColumnIndex(MySQLiteOpenHelper.CL_Verb));
				// tam thoi minh chi log du lieu ra cac ban co the xu ly du lieu
				// nay 1 cac tuy y
				Log.d(TAG, "id: " + id + " name: " + name + " add: " + Address);
			}
		}

		dataHelper.CloseBD();
		String[] from = { MySQLiteOpenHelper.CL_Name,
				MySQLiteOpenHelper.CL_Verb };
		int[] to = { R.id.tvName, R.id.tvVerb };

		adapter = new SimpleCursorAdapter(this, R.layout.layout_item, cursor,
				from, to);
		listDrugs.setAdapter(adapter);
		listDrugs.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long id) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					Intent newActivity = new Intent(MainDrugsActivity.this,
							Annotate.class);
					startActivity(newActivity);
					break;
				case 1:
					Intent newActivity1 = new Intent(MainDrugsActivity.this,
							Annotate.class);
					startActivity(newActivity1);
					break;
				case 2:
					Intent newActivity2 = new Intent(MainDrugsActivity.this,
							Annotate.class);
					startActivity(newActivity2);
					break;
				}
			}
		});
		

	}

	private void LoadData() {
		// TODO Auto-generated method stub
		Drugs mDrugs = new Drugs();
		mDrugs.setName("Tan");
		mDrugs.setVerb("TPHCM Viet Nam");
		dataHelper.INSERT_Drugs(mDrugs);
		Drugs mDrugs1 = new Drugs(0, "Emla", "");
		dataHelper.INSERT_Drugs(mDrugs1);
		Drugs mDrugs2 = new Drugs(0, "Lidocain-2%", "");
		dataHelper.INSERT_Drugs(mDrugs2);
		Drugs mDrugs3 = new Drugs(0, "Lidocaine 2% Epinephrine Normon", "");
		dataHelper.INSERT_Drugs(mDrugs3);
		Drugs mDrugs4 = new Drugs(0, "Mastu S", "");
		dataHelper.INSERT_Drugs(mDrugs4);
		Drugs mDrugs5 = new Drugs(0, "Medicaine-2%", "");
		dataHelper.INSERT_Drugs(mDrugs5);
		Drugs mDrugs6 = new Drugs(0, "Aminoleban-10%", "");
		dataHelper.INSERT_Drugs(mDrugs6);
		Drugs mDrugs7 = new Drugs(0, "Aminoplasmal Hepa-500ml", "");
		dataHelper.INSERT_Drugs(mDrugs7);
		Drugs mDrugs8 = new Drugs(0, "Ancatrol-0,25 mcg", "");
		dataHelper.INSERT_Drugs(mDrugs8);
		Drugs mDrugs9 = new Drugs(0, "Becombion Injection-2ml", "");
		dataHelper.INSERT_Drugs(mDrugs9);
		Drugs mDrugs10 = new Drugs(0, "Beedrafcin-0,25mcg", "");
		dataHelper.INSERT_Drugs(mDrugs10);
		Drugs mDrugs11 = new Drugs(0, "Becombion Injection-2ml", "");
		dataHelper.INSERT_Drugs(mDrugs11);
		Drugs mDrugs12 = new Drugs(0, "Alaxan", "");
		dataHelper.INSERT_Drugs(mDrugs12);
		Drugs mDrugs13 = new Drugs(0, "Coldfed-400mg", "");
		dataHelper.INSERT_Drugs(mDrugs13);
		Drugs mDrugs14 = new Drugs(0, "Hapacol 650mg", "");
		dataHelper.INSERT_Drugs(mDrugs14);
		Drugs mDrugs15 = new Drugs(0, "Panadol Children Soluble-250mg", "");
		dataHelper.INSERT_Drugs(mDrugs15);
		Drugs mDrugs16 = new Drugs(0, "Panadol Extra", "");
		dataHelper.INSERT_Drugs(mDrugs16);
		Drugs mDrugs17 = new Drugs(0, "Panadol viên sủi", "");
		dataHelper.INSERT_Drugs(mDrugs17);
		Drugs mDrugs18 = new Drugs(0, "Paracetamol-500mg", "");
		dataHelper.INSERT_Drugs(mDrugs18);
		Drugs mDrugs19 = new Drugs(0, "PCrotamiton", "");
		dataHelper.INSERT_Drugs(mDrugs19);
		Drugs mDrugs20 = new Drugs(0, "Eurax cream", "");
		dataHelper.INSERT_Drugs(mDrugs20);
		Drugs mDrugs21 = new Drugs(0, "Alaxan", "");
		dataHelper.INSERT_Drugs(mDrugs21);
		Drugs mDrugs22 = new Drugs(0, "Loxoprofen", "");
		dataHelper.INSERT_Drugs(mDrugs22);
		Drugs mDrugs23 = new Drugs(0, "Allopurinol", "");
		dataHelper.INSERT_Drugs(mDrugs23);
		Drugs mDrugs24 = new Drugs(0, "Ambroxol 30mg", "");
		dataHelper.INSERT_Drugs(mDrugs24);
		Drugs mDrugs25 = new Drugs(0, "Becotide", "");
		dataHelper.INSERT_Drugs(mDrugs25);
		Drugs mDrugs26 = new Drugs(0, "Bricanyl", "");
		dataHelper.INSERT_Drugs(mDrugs26);
		Drugs mDrugs27 = new Drugs(0, "CA C100 Sandoz", "");
		dataHelper.INSERT_Drugs(mDrugs27);
		Drugs mDrugs28 = new Drugs(0, "Calcium Corbiere 10ml", "");
		dataHelper.INSERT_Drugs(mDrugs28);
		Drugs mDrugs29 = new Drugs(0, "Calcium Sandoz 500mg", "");
		dataHelper.INSERT_Drugs(mDrugs29);
		Drugs mDrugs30 = new Drugs(0, "Acemuc", "");
		dataHelper.INSERT_Drugs(mDrugs30);
		Drugs mDrugs31 = new Drugs(0, "Actifed", "");
		dataHelper.INSERT_Drugs(mDrugs31);
		Drugs mDrugs32 = new Drugs(0, "Atussin", "");
		dataHelper.INSERT_Drugs(mDrugs32);
		Drugs mDrugs33 = new Drugs(0, "Acizit", "");
		dataHelper.INSERT_Drugs(mDrugs33);
		Drugs mDrugs34 = new Drugs(0, "Dextrex plus", "");
		dataHelper.INSERT_Drugs(mDrugs34);
		Drugs mDrugs35 = new Drugs(0,"E.E.S", "");
		dataHelper.INSERT_Drugs(mDrugs35);
		Drugs mDrugs36 = new Drugs(0, "Colestid", "");
		dataHelper.INSERT_Drugs(mDrugs36);
		Drugs mDrugs37 = new Drugs(0, "Lescol/lescol XL", "");
		dataHelper.INSERT_Drugs(mDrugs37);
		Drugs mDrugs38 = new Drugs(0, "Lofibra", "");
		dataHelper.INSERT_Drugs(mDrugs38);
		Drugs mDrugs39 = new Drugs(0, "Aspilets EC", "");
		dataHelper.INSERT_Drugs(mDrugs39);
		Drugs mDrugs40 = new Drugs(0, "Imdur", "");
		dataHelper.INSERT_Drugs(mDrugs40);
		Drugs mDrugs41 = new Drugs(0, "Plavix", "");
		dataHelper.INSERT_Drugs(mDrugs41);
		Drugs mDrugs42 = new Drugs(0, "Priorix", "");
		dataHelper.INSERT_Drugs(mDrugs42);
		Drugs mDrugs43 = new Drugs(0, "ROR - Trimovax", "");
		dataHelper.INSERT_Drugs(mDrugs43);
		Drugs mDrugs44 = new Drugs(0, "Colace", "");
		dataHelper.INSERT_Drugs(mDrugs44);
		Drugs mDrugs45 = new Drugs(0, "Sorbitol 5g", "");
		dataHelper.INSERT_Drugs(mDrugs45);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_drugs, menu);
		return true;
	}

}
